"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : MOT
Case Name   : 索引定义相关SQL，创建MOT表，创建索引
Create At   : 2020/09/18
Owner       : opentestcase028
Description :
    1.设置enable_incremental_checkpoint参数为off,并重启数据库
    2.创建MOT表，普通索引、唯一索引、指定索引、重命名索引，设置索引不可用等，清理环境
    3.恢复环境默认配置，修改enable_incremental_checkpoint为on，并重启数据库
Expect      :
    1.设置成功，重启数据库成功
    2.普通、唯一索引成功，指定索引成功，重命名索引失败，设置索引不可用失败，清理环境成功
    3.修改参数成功，重启数据库成功
History     :
    modified by opentestcase042,2023.01.05，重命名mot索引失败，设置mot索引不可用失败，适配代码
"""

import os
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class Mot_DDL_test(unittest.TestCase):

    def setUp(self):
        self.logger = Logger()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.commonsh = CommonSH
        self.common = Common()
        self.config = 'enable_incremental_checkpoint'
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.logger.info('------------修改配置，并重启数据库------------')
        show_msg = self.common.show_param(self.config)
        if 'off' not in show_msg:
            msg1 = self.primary_sh.execute_gsguc('set',
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            f'{self.config}=off')
            self.logger.info(msg1)
            msg2 = self.primary_sh.restart_db_cluster()
            self.logger.info(msg2)
            msg3 = self.primary_sh.get_db_cluster_status()
            self.assertTrue("Normal" or "Degrade" in msg3)

    def test_mot_index_DDL(self):
        text = '-----step:测试索引定义相关SQL，创建MOT表，创建唯一索引、指定索引等，删除索引、清理数据;' \
               '              expect:普通、唯一索引成功，指定索引成功，重命名索引失败，设置索引不可用失败，清理环境成功-----'
        self.t_name = 't_mot_0060'
        self.sql_cmd1 = f'''
                            drop foreign table if exists {self.t_name};
                            create foreign table {self.t_name}(id int not null,name varchar(10) not null);
                            create index id_index1 on {self.t_name}(id);
                            create unique index id_index2 on {self.t_name}(id);
                            create index id_index3 on {self.t_name} using btree(id);
                            create index id_index4 on {self.t_name}(id);
                            create index id_index5 on {self.t_name}(id);
                            create index id_index6 on {self.t_name}(id);
                            create index id_index7 on {self.t_name}(id);
                            create index id_index8 on {self.t_name}(id);
                            create index id_index9 on {self.t_name}(id);
                          '''
        msg4 = self.primary_sh.execut_db_sql(self.sql_cmd1)
        self.logger.info(msg4)
        self.assertIn(self.constant.CREATE_FOREIGN_SUCCESS_MSG, msg4)
        self.assertIn(self.constant.CREATE_INDEX_SUCCESS, msg4)
        self.sql_cmd2 = f'''
                            alter index id_index1 rename to id_index_new;
                            alter index id_index2 unusable;
                            drop index id_index3;
                            drop foreign table {self.t_name};
                          '''
        msg5 = self.primary_sh.execut_db_sql(self.sql_cmd2)
        self.logger.info(msg5)
        self.assertIn('Rename operation is not supported for memory table', msg5)
        self.assertIn('Unusable operation is not supported for memory table', msg5)
        self.assertIn(self.constant.DROP_INDEX_SUCCESS_MSG, msg5)
        self.assertIn(self.constant.DROP_FOREIGN_SUCCESS_MSG, msg5)

    def tearDown(self):
        self.logger.info('------------this is tearDown-------------')
        self.logger.info('-----------恢复配置，enable_incremental_checkpoint=on-----------')
        show_msg = self.common.show_param(self.config)
        if 'on' not in show_msg:
            msg6 = self.primary_sh.execute_gsguc('set',
                                                 self.constant.GSGUC_SUCCESS_MSG,
                                                 f'{self.config}=on')
            self.logger.info(msg6)
            msg7 = self.primary_sh.restart_db_cluster()
            self.logger.info(msg7)
            msg8 = self.primary_sh.get_db_cluster_status()
            self.assertTrue("Normal" or "Degrade" in msg8)
        show_msg = self.common.show_param(self.config)
        self.assertTrue('on' in show_msg)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
